package org.zxp.encode;

import java.io.UnsupportedEncodingException;
import java.util.stream.Stream;

/**
 * @program: effectiveJava
 * @description: 测试UTF-8下的信息解码为字节的情况
 * @author: X-Pacific zhang
 * @create: 2019-06-03 11:06
 **/
public class TestEncode {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String encode = "我是个大好人";
        System.out.println(encode.getBytes().length);//18 utf8下一个中文占3个字节
        encode += "A";
        System.out.println(encode.getBytes().length);//19 utf8下一个字母占3个字节

        //编码过程 字符-字节
        encode.getBytes();
        encode.getBytes("UTF-8");
        encode.getBytes("GBK");

        //解码过程 字节-字符
        String decode = new String(encode.getBytes(),"utf-8");
        System.out.println(decode);

        //乱码原因1 长度不够
        String decode2 = new String(encode.getBytes(),0,encode.length()-3,"utf-8");
        System.out.println(decode2);
        //乱码原因2 编码不对
        String decode3 = new String(encode.getBytes(),"GBK");
        System.out.println(decode3);
    }
}
